import { defineStore } from "pinia";
import { totalProductPagePost, totalProductDeleteDel,totalProductDetailGet } from "@/api/allProduct"
import { useEnumStore } from "@/store/enumeration"
import { ElMessage } from "element-plus";
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';

export const allTotalProListStore = defineStore("allTotalProListStoreId", () => {
    const { allProductType } = useEnumStore()
    const { global } = useInstance();

    const detailData = ref([
        {
            type: "text",
            prop: "supplierType",
            name: "供应商类型",
            value: null
        },

    ])
    const tableProp = reactive(
        {
            name: "allTotalPro",
            propsList: [

                {
                    prop: 'id',
                    label: '序号',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'productCategoryName',
                    label: '名称',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'productCategoryCode',
                    label: '编码',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                // {
                //     prop: 'productCode',
                //     label: '产品编码',
                //     type: 'normal',
                //     minWidth: '200px',
                //     hidden: true,
                // },
                {
                    prop: 'productType',
                    label: '类型',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    prop: 'productCode',
                    label: '附件图片',
                    type: 'image',
                    minWidth: '200px',
                    hidden: true,
                },

                {
                    prop: 'remarks',
                    label: '备注',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },

                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive({
        orders: ["id"],
        condition: {
            productCategoryName: null,//产品总称 
            productCategoryCode: null,//产品编码 
            productType: null,//产品类型 
            remarks: null,//供应商编码 
            id: null,//序号 
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],

    })
    const addEditRef = ref<{ show: (title: string, row?: any) => void }>()
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()

    const getData1 = async (type: string) => {

        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                listSearch.condition[it] = null
            })
        }
        try {
            const res = await totalProductPagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records.map((item: any) => {

                    return {
                        ...item,
                        productType: allProductType[item.productType -1]?.value
                    }
                })
                pageParams.total = res.data.total
                pageParams.loadIng = false
            }
        } catch (error) {
            pageParams.loadIng = false
            pageParams.tableData = []
            pageParams.total = 0
        }finally{
            mittBus.emit('scrollUpdate')
        }

    }
    const handPage = (page: any) => {
        listSearch.pageNum = page.pageNum
        listSearch.pageSize = page.pageSize
        getData1('search')
    }
    const AddEditBtn = async (type: string, row?: any) => {
        if (type == 'add') {
            addEditRef.value?.show('add')
        } else {
            const res = await totalProductDetailGet({id:row.id})
            console.log(addEditRef.value);
            
            addEditRef.value?.show('edit', res.data)
        }
    }
    const detailBtn = async ( row: any) => {
   
            const res = await totalProductDetailGet({id:row.id})
            
            detailRef.value?.show('edit', res.data)
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.productCategoryName}`)
        if (flag) {
            const res = await totalProductDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getData1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    
    return {
        detailData, tableProp, listSearch, pageParams, addEditRef,detailRef,
        getData1, handPage, AddEditBtn, delBtn,detailBtn
    }
})